package com.tebiecloud.yunst.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.yunst.client.model.entity.YunAgentCollect;
import com.tebiecloud.yunst.server.mapper.YunAgentCollectMapper;
import com.tebiecloud.yunst.server.service.YunAgentCollectService;
import com.tebiecloud.yunst.server.yunstservice.YunStService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 *  服务实现类
 * @author tebie
 * @date 2019-09-07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class YunAgentCollectServiceImpl extends BaseServiceImpl<YunAgentCollectMapper, YunAgentCollect> implements YunAgentCollectService {

    @Value("${tebiecloud.yunst.backUrl}")
    private String backUrl;

    @Autowired
    YunStService yunStService;

    @Autowired
    YunAgentCollectMapper yunAgentCollectMapper;

    @Override
    public HashMap<String, String> agentCollect(String payerId, String bizUserId, Long orderNo, Integer amount, String payMethodStr, String wxOpenId) {
        HashMap<String, String> resultMap = new HashMap<String, String>();
        String newbackUrl = backUrl +"/order/ordercallback/consumeApplyCallBack?orderId="+orderNo;
        resultMap = yunStService.agentCollectApplySimplify(payerId,bizUserId,amount.longValue(),newbackUrl,payMethodStr,wxOpenId,null);
        //获取正确，插入预支付表
        if(resultMap.get("status").equals("OK")){
            YunAgentCollect yunAgentCollect = new YunAgentCollect();
            yunAgentCollect.setAmount(amount);
            yunAgentCollect.setBizOrderNo(resultMap.get("bizOrderNo"));
            yunAgentCollect.setOrderId(orderNo);
            yunAgentCollect.setPayerId(payerId);
            yunAgentCollect.setRecieverId(bizUserId);
            yunAgentCollect.setPayStatus(0);
            yunAgentCollect.setRemark(resultMap.get("res"));
            yunAgentCollectMapper.insert(yunAgentCollect);
        }
        return resultMap;
    }

    @Override
    public HashMap<String, String> consumeApply(String payerId, String bizUserId, Long orderId, Long userId,
                                                Integer amount, String payMethodStr, String openId,int expiredTime) {
        HashMap<String, String> resultMap = new HashMap<String, String>();
        //回调接口
        String newbackUrl = backUrl+"/order/ordercallback/consumeApplyCallBack?orderId="+orderId+"&userId="+userId;
        resultMap = yunStService.consumeApply(payerId,bizUserId,amount.longValue(),newbackUrl,payMethodStr,openId,expiredTime);
        //获取正确，插入预支付表
        if(StringUtils.isNotEmpty(resultMap.get("status")) && resultMap.get("status").equals("OK")){
            YunAgentCollect yunAgentCollect = new YunAgentCollect();
            yunAgentCollect.setAmount(amount);
            yunAgentCollect.setBizOrderNo(resultMap.get("bizOrderNo"));
            yunAgentCollect.setOrderId(orderId);
            yunAgentCollect.setPayerId(payerId);
            yunAgentCollect.setRecieverId(bizUserId);
            yunAgentCollect.setPayStatus(0);
            yunAgentCollect.setAppId(resultMap.get("appId"));
            yunAgentCollect.setPrepayId(resultMap.get("prepayid"));
            yunAgentCollect.setNonceStr(resultMap.get("nonceStr"));
            yunAgentCollect.setPackageStr(resultMap.get("packageStr"));
            yunAgentCollect.setTimeStamp(resultMap.get("timeStamp"));
            yunAgentCollect.setSignType(resultMap.get("signType"));
            yunAgentCollect.setPaySign(resultMap.get("paySign"));
            yunAgentCollect.setRemark(resultMap.get("res"));
            yunAgentCollectMapper.insert(yunAgentCollect);
        }
        return resultMap;
    }

    @Override
    public int payCallBack(String bizOrderNo, String status) {
        UpdateWrapper<YunAgentCollect> yunAgentCollectUpdateWrapper = new UpdateWrapper<>();
        yunAgentCollectUpdateWrapper.eq("biz_order_no",bizOrderNo);
        YunAgentCollect yunAgentCollect = new YunAgentCollect();
        if(status.equals("OK")) {
            yunAgentCollect.setPayStatus(1);
        }else{
            yunAgentCollect.setPayStatus(2);
        }
        return yunAgentCollectMapper.update(yunAgentCollect,yunAgentCollectUpdateWrapper);
    }

    @Override
    public void applicationTransfer(String bizUserId, Long amount) {
        yunStService.applicationTransfer(bizUserId,amount);
    }

    @Override
    public HashMap<String, String> queryBalance(String bizUserId) {
        yunStService.queryBalance(bizUserId);
        return null;
    }
}
